package com.lyx.bluetoothdemo

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import pub.devrel.easypermissions.EasyPermissions

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        permissionsInit()
        initView()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            22 -> {
                if (resultCode == RESULT_OK) {
                    Toast.makeText(this, "已开启蓝牙！", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "请开启蓝牙！", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    private fun initView() {
        findViewById<TextView>(R.id.init_bluetooth).setOnClickListener {
            bluetoothInit()
        }
        findViewById<TextView>(R.id.bonded_devices).setOnClickListener {
            BluetoothHelper.getBondedDevices()
        }
    }

    private fun bluetoothInit() {
        when (BluetoothHelper.bluetoothAdapterInit(this)) {
            is BluetoothNull -> {
                showAlert("提示", "设备不支持蓝牙，软件无法使用")
                return
            }
            is BluetoothPermission -> {
                showAlert("提示", "请先打开系统定位开关，再重新启动应用")
                return
            }
            is BluetoothNotOpen -> {
                // 方案一
                // BluetoothHelper.openBluetoothSetting()

                // 方案二
                val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                startActivityForResult(enableBtIntent, 22)
                return
            }
            is BluetoothSuccess -> {

            }
        }
    }

    private fun showAlert(title: String, content: String) {
        runOnUiThread {
            AlertDialog.Builder(this)
                .setTitle(title)
                .setMessage(content)
                .setPositiveButton(
                    "确定",
                    null
                )
                .setCancelable(false)
                .create().show()
        }
    }


    //申请权限
    private fun permissionsInit() {
        //申请位置权限
        val perms = arrayOf(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        )
        if (EasyPermissions.hasPermissions(this, *perms)) {
            // Already have permission, do the thing
        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(
                this, "请先授权应用定位权限",
                0, *perms
            );
        }
    }

}